package xyz.lwm.lazycat.web.route;

import lombok.Getter;
import xyz.lwm.lazycat.web.handle.Handler;
import xyz.lwm.lazycat.web.handle.HandlerType;

/**
 * 变量化路由, 请求路径中存在变量
 *
 * @author lwm
 */
@Getter
class VariableRoute extends AbstractRoute {

    /**
     * path 中存在变量
     *
     * @param path 路径
     * @return 是否存在变量
     */
    public static boolean isVariational(String path) {
        return path.contains("{");
    }

    private final String regex;

    public VariableRoute(String path, HandlerType type, Handler handler) {
        super(path, type, handler);
        // users/{id}/{code} -> users/\\w+/\\w+
        this.regex = path.replaceAll("\\{\\w+}", "\\\\w+");
    }

    @Override
    public String getKey() {
        return regex;
    }

    @Override
    public boolean matches(String path) {
        return path.matches(regex);
    }

}
